package com.fancy.learncenter.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.ShowVideoOrAudioDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.adapter.FragmentShowVideoAdapter;
import com.fancy.learncenter.ui.view.CustomFootView;
import com.fancy.learncenter.ui.view.CustomHeaderView;
import com.fancy.learncenter.ui.view.LinearIiemDecoration;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShowVideoFragment extends Fragment {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    View rootView;

    List<ShowVideoOrAudioDataBean.RecommendListBean> listData;
    FragmentShowVideoAdapter listAdapter;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;


    int curPage = 1;
    int pageSize = 10;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_show_video, container, false);
        ButterKnife.bind(this, rootView);

        initRecycleView();

        getData(true);


        return rootView;
    }

    private void initRecycleView() {
        listData = new ArrayList<>();

        xRefreshView.setPullLoadEnable(true);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new LinearIiemDecoration(getActivity()));
        listAdapter = new FragmentShowVideoAdapter(getActivity(), listData);
        recyclerView.setAdapter(listAdapter);

        listAdapter.setCustomLoadMoreView(new CustomFootView(getActivity()));

        xRefreshView.setCustomHeaderView(new CustomHeaderView(getActivity()));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getData(false);
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });
        
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void getData(final boolean refresh) {
        Map<String, Object> paramer = new HashMap<>();
        paramer.put("type", "video");

        if (refresh) {
            curPage = 1;
        } else {
            curPage++;
        }

        paramer.put("curPage", curPage);
        paramer.put("pageSize", pageSize);

        HttpMehtod.getInstance().getShowListData(paramer, new CustomNetSubscriber(getActivity()) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<ShowVideoOrAudioDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), ShowVideoOrAudioDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<ShowVideoOrAudioDataBean>() {
                    @Override
                    public void success(ShowVideoOrAudioDataBean dataBean) {

                        if (refresh) {
                            listAdapter.notifyDataSetChanged(dataBean.getRecommendList());
                            xRefreshView.stopRefresh();
                            xRefreshView.setLoadComplete(false);
                        } else {
                            listAdapter.addAll(dataBean.getRecommendList());

                            if (dataBean.getRecommendList() == null) {
                                xRefreshView.setLoadComplete(true);
                            } else if (dataBean.getRecommendList() != null && dataBean.getRecommendList().size() < 10) {
                                xRefreshView.setLoadComplete(true);
                            } else {
                                xRefreshView.stopLoadMore();
                            }
                        }

                    }

                    @Override
                    public void fail(String errorCode) {

                    }

                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
            }
        });

    }
}
